package com.br.gpstracker.negocio;

import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
import android.widget.Toast;

import com.br.gpstracker.BroadcastSMSReceiver;
import com.br.gpstracker.negocio.Carro.NumeroAutorizado;

public class SMSGPSTrackerControle {
	private static final String STR_EMPTY = "";
	private static final String STR_SPACE = " ";

	private Context context;

	public SMSGPSTrackerControle(Context context) {
		this.context = context;
	}

	private void erro(String msg) {
		Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
	}

	private void sendTextMessage(String destinationAddress, String text) {
		SmsManager.getDefault().sendTextMessage(destinationAddress, null, text, null, null);

		Intent intent = new Intent(BroadcastSMSReceiver.ACTION_SMS_SENT);
		context.sendBroadcast(intent);
	}

	public void begin(Carro carro) {
		String destinationAddress = carro.getNumeroGSM();
		String text = "begin" + carro.getSenha();
		sendTextMessage(destinationAddress, text);
	}

	public void changePassword(Carro carro, String novoPassword) {
		if (novoPassword.length() != 6) {
			erro(STR_EMPTY);
			return;
		}
		String destinationAddress = carro.getNumeroGSM();
		String text = "password" + carro.getSenha() + STR_SPACE + novoPassword;
		sendTextMessage(destinationAddress, text);
	}

	public void autorizacao(Carro carro, NumeroAutorizado numeroAutorizado) {
		String destinationAddress = carro.getNumeroGSM();
		String text = "admin" + carro.getSenha() + STR_SPACE + numeroAutorizado.getNumero();
		sendTextMessage(destinationAddress, text);
	}
}
